<?php
/**
 * Library Of Shared Code (LOSC)
 *
 * LICENSE
 *
 * This source file is subject to the New BSD License that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   LOSC Framework
 * @package    Controller
 * @subpackage Plugin
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */

/**
 * Plugin for extending session lifetime if "remember me" is on
 *
 * @category   LOSC Framework
 * @package    Controller
 * @subpackage Plugin
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */
class Losc_Controller_Plugin_SessionRemember
    extends Zend_Controller_Plugin_Abstract
{
    const SECONDS = 15778463;
    
    /**
     * Extends session lifetime if "remember me" is on
     *
     * @return void
     */
    //public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    public function dispatchLoopShutdown()
    {
        if (!Zend_Session::isStarted()) {
            return;
        }
        
        if (isset($_SESSION['remember']) && $_SESSION['remember']) {
            // update lifetime in save handler
            $sh = Zend_Session::getSaveHandler();
            $sh->setLifetime(self::SECONDS, true);
            
            // session_set_cookie_params() doesn't work when session is started,
            // so here's a hack that overwrites the cookie manually
            $cookieParams = session_get_cookie_params();
            $name = session_name();
            setcookie($name,
                      $_COOKIE[$name],
                      time() + self::SECONDS,
                      $cookieParams['path'],
                      $cookieParams['domain'],
                      $cookieParams['secure']);
        }
    }
}
